package com.liqi.web.controller.openapi.middleware;

import com.liqi.common.config.Sm4TypeHandler;
import com.liqi.common.core.domain.R;
import com.liqi.common.core.domain.entity.SysUser;
import com.liqi.deployment.domain.DvOperationUser;
import com.liqi.system.mapper.DvOperationUserMapper;
import com.liqi.system.mapper.SysUserMapper;
import com.liqi.system.service.device.IDvOperationUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: lgh
 * @Date: 2023-05-31
 * @Description:
 */
@RestController
@RequestMapping("/openApi")
@Api(tags = "数据脱敏,使用之前一定要备份数据库，默认密码为1221")
public class DataDesensitizationController {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DvOperationUserMapper DvOperationUserMapper;


    @GetMapping("/userDataDes")
    @ApiOperation("用户数据脱敏")
    @Transactional
    public R userDataDes(@RequestParam String pwd) {
        if (!"1221".equals(pwd)) {
            return R.fail("密码不对,请确认数据库已经备份");
        }
        List<SysUser> sysUsers = sysUserMapper.selectUserList(new SysUser(),Sm4TypeHandler.KEY );
        sysUsers.forEach(m -> {
//            m.setNickName(!StringUtils.isBlank(m.getNickName()) ? SmUtil.sm4(KEY.getBytes()).encryptHex(m.getNickName()):null);
//            m.setEmail(!StringUtils.isBlank(m.getEmail()) ? SmUtil.sm4(KEY.getBytes()).encryptHex(m.getEmail()): null);
//            m.setPhonenumber(!StringUtils.isBlank(m.getPhonenumber()) ? SmUtil.sm4(KEY.getBytes()).encryptHex(m.getPhonenumber()):null);
            sysUserMapper.updateUser(m);
        });
        return R.ok();
    }

    @GetMapping("/operationUser")
    @ApiOperation("设备管理员数据脱敏")
    @Transactional
    public R operationUser(@RequestParam String pwd) {
        if (!"1221".equals(pwd)) {
            return R.fail("密码不对,请确认数据库已经备份");
        }
        List<DvOperationUser> dvOperationUsers = DvOperationUserMapper.selectDvOperationUserList(new DvOperationUser(),Sm4TypeHandler.KEY);
        dvOperationUsers.forEach(m ->{
            DvOperationUserMapper.updateDvOperationUser(m);
        });
        return R.ok();
    }
}
